<?php
//抽象命令类
interface Command{
	public function execute();
}
//具体命令类
class BuyCommand implements Command{
	
	private $receiver;
	
	public function __construct(Receiver $receiver){
		$this->receiver = $receiver;
	}
	
	public function execute(){
		$this->receiver->buy();
	}
}

class SellerCommand implements Command{

	private $receiver;

	public function __construct(Receiver $receiver){
		$this->receiver = $receiver;
	}

	public function execute(){
		$this->receiver->seller();
	}
}
//接收类
class Receiver{
	private $goods_name;
	
	public function __construct($goods_name){
		$this->goods_name = $goods_name;
	}
	
	public function buy(){
		echo '购买'.$this->goods_name.'<br/>';
	}
	
	public function seller(){
		echo '卖出'.$this->goods_name.'<br/>';
	}
}
//调用类
class Invoker{
	
	private $command;
	
	public function __construct($command){
		$this->command = $command;
	}
	
	public function excuteComand(){
		$this->command->execute();
	}
}
$receiver = new Receiver('玩具'); 
$invoker = new Invoker(new BuyCommand($receiver));
$invoker->excuteComand();

$invoker = new Invoker(new SellerCommand($receiver));
$invoker->excuteComand();
